package com.babyqiao.wallpaper.manager.controller;

import com.babyqiao.wallpaper.manager.controller.dto.CategoryPicQuery;
import com.babyqiao.wallpaper.manager.entity.WallpaperCategoryPic;
import com.babyqiao.wallpaper.manager.service.CategoryPicService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.babyqiao.wallpaper.common.UnifyResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类图片管理接口
 *
 * @author QLZ
 * @date 2020/12/16
 */
@Api(tags = "分类图片管理接口")
@RestController
@RequestMapping("/api/admin/categorys")
public class CategoryPicController {
    @Autowired
    private CategoryPicService categoryPicService;


    /**
     * 添加分类照片
     *
     * @Author QLZ
     * @Date 2020/12/17
     * * @param wallpaperCategoryPic:
     * * @return: com.babyqiao.wallpaper.com.babyqiao.wallpaper.common.UnifyResult
     */

    @ApiOperation(value = "添加分类照片", httpMethod = "POST")
    @PostMapping("/category/pics")
    public UnifyResult addPic(@ApiParam(name = "id", value = "照片参数", required = true) @RequestBody List<WallpaperCategoryPic> wallpaperCategoryPic) {

        boolean save = categoryPicService.saveBatch(wallpaperCategoryPic);
        return UnifyResult.success();
    }

    /**
     * 多条件分页查询图片
     *
     * @param limit:
     * @param categoryPicQuery: * @return: com.babyqiao.wallpaper.com.babyqiao.wallpaper.common.UnifyResult
     * @Author QLZ
     * @Date 2020/12/17
     * * @param current:
     */


    @ApiOperation(value = "多条件分页查询图片", httpMethod = "POST")
    @PostMapping("/category/{current}/{limit}/pics")
    public UnifyResult getPics(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable long current,
                               @ApiParam(name = "limit", value = "每页显示条数", required = true) @PathVariable long limit,
                               @ApiParam(name = "categoryPicQuery", value = "查询参数") @RequestBody(required = false) CategoryPicQuery categoryPicQuery) {

        Page<WallpaperCategoryPic> page = new Page<>(current, limit);
        QueryWrapper<WallpaperCategoryPic> queryWrapper = categoryPicService.addSelectCondition(categoryPicQuery);
        categoryPicService.page(page, queryWrapper);
        long total = page.getTotal();
        List<WallpaperCategoryPic> records = page.getRecords();


        return UnifyResult.success().data("result", records).data("total", total);
    }

    /**
     * 修改上下线状态
     *
     * @param state: 状态
     *               * @return: com.babyqiao.wallpaper.com.babyqiao.wallpaper.common.UnifyResult
     * @Author QLZ
     * @Date 2020/12/17
     * * @param id: 图片ID
     */

    @ApiOperation(value = "修改上下线状态", httpMethod = "GET")
    @GetMapping("/online/{id}/{state}")
    public UnifyResult modifiyPicOnline(@ApiParam(name = "id", value = "图片ID", required = true) @PathVariable String id,
                                        @ApiParam(name = "state", value = "上线状态（0=false 1=true", required = true) @PathVariable Integer state) {
        UpdateWrapper<WallpaperCategoryPic> updateWrapper = categoryPicService.addUpdataCondition(id, state);
        boolean update = categoryPicService.update(updateWrapper);
        if (update) {
            return UnifyResult.success();
        }
        return UnifyResult.error();
    }

    @ApiOperation(value = "删除图片", httpMethod = "DELETE")
    @DeleteMapping("{id}")
    public UnifyResult deletePic(@PathVariable String id) {
        boolean b = categoryPicService.removeById(id);
        if (b) {
            return UnifyResult.success();
        }
        return UnifyResult.error();

    }
}

